package view;



import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import model.Chamado;
import model.Equipamento;
import control.ControllersDaos;

public class VChamado2 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5434087669693995477L;
	private JPanel contentPane;
	private JTextField tf_num_cham;
	private JTextField tf_ID_Aten;
	private JTextField tf_ID_Tec;
	private JTextField tf_ID_Solic;
	private JComboBox <String>cboxEquip = new JComboBox<>();
	private JTextArea textArea_prob;
	private JTextArea textArea_soluc;
	private VChamado telaAntCham;
	private VPrincipal telaAnt;
	private JButton btnLimpar;
	private JButton btnFecharChamado;
	private JButton btnConfirmar;
	//private ChamadoCRUD  chamadoCrud  =  new ChamadoCRUD();
	//private EquipamentoCRUD EquipCrud  =  new EquipamentoCRUD();
	private ControllersDaos controller;
	private Chamado chamadoIn = new Chamado();

	/**
	 * Launch the application.
	 */
	/*
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VChamado2 frame = new VChamado2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	private void LimparTela() {
		
		tf_ID_Aten.setText("");
		tf_ID_Solic.setText("");
		tf_ID_Tec.setText("");
		textArea_prob.setText("");
		textArea_soluc.setText("");


	}

	private void BloquearCampos() {
		tf_num_cham.setEnabled(false);
		tf_ID_Aten.setEnabled(false);
		tf_ID_Solic.setEnabled(false);
		tf_ID_Tec.setEnabled(false);

		textArea_prob.setEnabled(false);
		textArea_soluc.setEnabled(false);
		
		cboxEquip.setEnabled(false);

	}

	
	/**
	 * Create the frame.
	 */
	public VChamado2(VPrincipal telaAnterior, ControllersDaos controlador) {
        this();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        telaAnt    = telaAnterior;
        controller = controlador;
        
		try{
			//tf_num_cham.setText(String.valueOf(chamadoCrud.proximoIdChamado()));
			tf_num_cham.setText(String.valueOf(controller.proximoIdChamado()));
			ArrayList<Equipamento> listEquip = new ArrayList<Equipamento>();
			//listEquip = EquipCrud.listarEquipamento();
			listEquip = controller.listarEquipamento();
			cboxEquip.removeAllItems();
			for (Equipamento equip : listEquip) {
				cboxEquip.addItem(equip.getNumSerie() + " - "+"Tipo: "
						+ equip.getTipo().toUpperCase());
			}
			
    	} catch (ClassNotFoundException ex) {
    		JOptionPane.showMessageDialog(null, "Problema na solicita\u00E7\u00E3oo do Chamado");
    		ex.printStackTrace();
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(null, "Problema na solicita\u00E7\u00E3oo do Chamado no Banco.");
			ex.printStackTrace();
		}
	
	}
	
	public VChamado2() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				if(telaAntCham!= null){
					telaAntCham.repaint();
					telaAntCham.AtualizarTable();
//					telaAntCham.telaAnt.setVisible(true);
					telaAntCham.setVisible(true);
				}else{
					telaAnt.setVisible(true);	
				}
				
			}
		});
		
		VChamado2.this.setResizable(false);
		VChamado2.this.setLocationRelativeTo(null);
		setTitle("Chamado");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 462, 445);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbl_Num_Cham = new JLabel("N\u00BA Chamado");
		lbl_Num_Cham.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lbl_Num_Cham.setBounds(10, 24, 89, 14);
		contentPane.add(lbl_Num_Cham);
		
		JLabel lbl_atend = new JLabel("Id Atendente");
		lbl_atend.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lbl_atend.setBounds(120, 24, 89, 14);
		contentPane.add(lbl_atend);
		
		JLabel lblIdTecnico = new JLabel("Id Tecnico");
		lblIdTecnico.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lblIdTecnico.setBounds(230, 24, 89, 14);
		contentPane.add(lblIdTecnico);
		
		JLabel lblNumSerieEquipamento = new JLabel("Num Serie - Tipo");
		lblNumSerieEquipamento.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lblNumSerieEquipamento.setBounds(10, 80, 188, 14);
		contentPane.add(lblNumSerieEquipamento);
		
		JLabel lblDescrioDoProblema = new JLabel("Descri\u00E7\u00E3o do Problema");
		lblDescrioDoProblema.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lblDescrioDoProblema.setBounds(10, 129, 408, 14);
		contentPane.add(lblDescrioDoProblema);
		
		tf_num_cham = new JTextField();
		tf_num_cham.setEditable(false);
		tf_num_cham.setBounds(10, 49, 78, 20);
		contentPane.add(tf_num_cham);
		tf_num_cham.setColumns(10);
		
		tf_ID_Aten = new JTextField();
		tf_ID_Aten.setBounds(120, 49, 89, 20);
		contentPane.add(tf_ID_Aten);
		tf_ID_Aten.setColumns(10);
		
		tf_ID_Tec = new JTextField();
		tf_ID_Tec.setBounds(230, 49, 78, 20);
		contentPane.add(tf_ID_Tec);
		tf_ID_Tec.setColumns(10);
		
		cboxEquip.setBounds(10, 98, 199, 20);
		contentPane.add(cboxEquip);
		
		
		textArea_prob = new JTextArea();
		textArea_prob.setBorder(new TitledBorder(null, "", TitledBorder.TRAILING, TitledBorder.TOP, null, null));
		textArea_prob.setBounds(10, 154, 434, 88);
		contentPane.add(textArea_prob);
		
		btnLimpar = new JButton("Limpar");
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LimparTela();
			}
		});
		btnLimpar.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		btnLimpar.setBounds(10, 373, 89, 23);
		contentPane.add(btnLimpar);
		
		btnConfirmar = new JButton("Confirmar");
		btnConfirmar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String campoIdCha = tf_num_cham.getText().trim();
				String campoIdAte = tf_ID_Aten.getText().trim();
				String campoIdTec = tf_ID_Tec.getText().trim();
				String campoIdSol = tf_ID_Solic.getText().trim();
				String campoDesc = textArea_prob.getText().trim();
				String[] i = cboxEquip.getSelectedItem().toString().trim()
						.split(" - ");
				String campoSerie = i[0];
				String campoStatus = "Aberto";
				int nIdCha,nIdAte,nIdTec,nIdSol,nSerie;

				if (campoIdCha == null || campoIdCha.equals("")
						|| campoIdAte == null || campoIdAte.equals("")
						|| campoIdTec == null || campoIdTec.equals("")
						|| campoIdSol == null || campoIdSol.equals("")
						|| campoDesc == null || campoDesc.equals("")
						|| campoSerie == null || campoSerie.equals(""))

				{
					JOptionPane.showMessageDialog(null,"Verifique todos campos");
				} else {
					try {
						nIdCha = Integer.parseInt(campoIdCha);
						nIdAte = Integer.parseInt(campoIdAte);
						nIdTec = Integer.parseInt(campoIdTec);
						nIdSol = Integer.parseInt(campoIdSol);
						nSerie = Integer.parseInt(campoSerie);
						Date data = new java.sql.Date(new java.util.Date().getTime());

						chamadoIn = new Chamado(nIdCha, nIdAte, nIdTec, nIdSol, nSerie, campoDesc, null, campoStatus, data );

						//chamadoCrud.inserirChamado(chamadoIn);
						controller.inserirChamado(chamadoIn);
						JOptionPane.showMessageDialog(null, "Chamado Cadastrado Com SUCESSO!!","Cadastro",JOptionPane.INFORMATION_MESSAGE);

						LimparTela();
						//tf_num_cham.setText(String.valueOf(chamadoCrud.proximoIdChamado()));
						tf_num_cham.setText(String.valueOf(controller.proximoIdChamado()));
						
					} catch (NumberFormatException ex) {
						JOptionPane.showMessageDialog(null,"Problema no Casdastro do Chamado, Verifique os Campos numéricos: "
								+ ex.getMessage());
					} catch (ClassNotFoundException ex) {
						JOptionPane.showMessageDialog(null,"Problema no Casdastro do Chamado: "
								+ ex.getMessage());
					} catch (SQLException ex) {
						JOptionPane.showMessageDialog(null,"Problema no Casdastro do Chamado no Banco: "
								+ ex.getMessage());
					}
				}
				
				
			}
		});
		btnConfirmar.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		btnConfirmar.setBounds(355, 373, 89, 23);
		contentPane.add(btnConfirmar);
		
		JLabel lblSoluo = new JLabel("Solu\u00E7\u00E3o");
		lblSoluo.setForeground(Color.BLUE);
		lblSoluo.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lblSoluo.setBounds(10, 252, 408, 14);
		contentPane.add(lblSoluo);
		
		textArea_soluc = new JTextArea();
		textArea_soluc.setEnabled(false);
		textArea_soluc.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		textArea_soluc.setBounds(10, 277, 434, 88);
		contentPane.add(textArea_soluc);
		
		JLabel lblIdSolicitante = new JLabel("Id Solicitante");
		lblIdSolicitante.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		lblIdSolicitante.setBounds(329, 24, 112, 14);
		contentPane.add(lblIdSolicitante);
		
		tf_ID_Solic = new JTextField();
		tf_ID_Solic.setColumns(10);
		tf_ID_Solic.setBounds(329, 49, 78, 20);
		contentPane.add(tf_ID_Solic);
		
		JLabel label = new JLabel("*");
		label.setForeground(Color.RED);
		label.setBounds(206, 25, 13, 14);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("*");
		label_1.setForeground(Color.RED);
		label_1.setBounds(298, 25, 13, 14);
		contentPane.add(label_1);
		
		JLabel label_3 = new JLabel("*");
		label_3.setForeground(Color.RED);
		label_3.setBounds(416, 25, 13, 14);
		contentPane.add(label_3);
		
		JLabel lblCamposNumrios = new JLabel("* Campos Num\u00E9rios");
		lblCamposNumrios.setForeground(Color.RED);
		lblCamposNumrios.setBounds(326, 101, 118, 14);
		contentPane.add(lblCamposNumrios);
		
	}

	public VChamado2(VChamado telaAnterior,Chamado chamado1In,ControllersDaos controlador) {
		    this();
	        this.setLocationRelativeTo(null);
	        this.setResizable(false);
	        telaAntCham = telaAnterior;
	        controller = controlador;
	        this.chamadoIn = chamado1In;
		
		 
		ArrayList<Chamado> listChamado = new ArrayList<Chamado>();
		try {
			
			//listChamado = chamadoCrud.listarChamado();
			listChamado = controller.listarChamado();
			for (Chamado chamado : listChamado) {
				if (chamadoIn.getIdChamado() == chamado.getIdChamado()) {
					tf_num_cham.setText(chamado.getIdChamado() + "");
					tf_ID_Aten.setText(chamado.getIdAtendente()+"");
					tf_ID_Tec.setText(chamado.getIdTecnico()+"");
					tf_ID_Solic.setText(chamado.getIdSolicitante()+"");
					cboxEquip.addItem(chamado.getNumSerieEquip()+"");
					textArea_prob.setText(chamado.getDescricao());
					chamadoIn = chamado;
				}
			}
			BloquearCampos();
			textArea_soluc.setEnabled(true);
			btnLimpar.setEnabled(false);
			btnConfirmar.setEnabled(false);
			AtivarBtnFecharChamado();
			
			
		} catch (NumberFormatException ex) {
			JOptionPane.showMessageDialog(null,"Problema na Pesquisa do Chamado: "
					+ ex.getMessage());
		} catch (ClassNotFoundException ex) {
			JOptionPane.showMessageDialog(null,"Problema na Pesquisa do Chamado: "
					+ ex.getMessage());
		} catch (NullPointerException ex) {
			JOptionPane.showMessageDialog(null,"Problema na Pesquisa do Chamado: "
					+ ex.getMessage());
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(null,"Problema na Pesquisa do Chamado: "
					+ ex.getMessage());
		}	
	}
	
	private void AtivarBtnFecharChamado() {
		btnFecharChamado = new JButton("Fechar Chamado");
		btnFecharChamado.setForeground(Color.BLUE);
		btnFecharChamado.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
		btnFecharChamado.setBounds(139, 374, 169, 23);
		contentPane.add(btnFecharChamado);
	
		btnFecharChamado.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (textArea_soluc.getText().trim()==null||textArea_soluc.getText().trim().equals("")){
					JOptionPane.showMessageDialog(null, "Especifique qual foi a solu\u00E7\u00E3o para este chamado");
				}else{
					//Chamado chamadoTmp = new Chamado();
					//String CampoNumCham = tf_num_cham.getText().trim();
					//int nNumChamado = 0;
					try {
						//nNumChamado = Integer.parseInt(CampoNumCham);
						//chamadoTmp.setIdChamado(nNumChamado);
						chamadoIn.setSolucao(textArea_soluc.getText().trim());
						chamadoIn.setStatus("Fechado");
						
						//chamadoCrud.alterarChamado(chamadoTmp);
						controller.alterarChamado(chamadoIn);
						
						JOptionPane.showMessageDialog(null,"Chamado Fechado Com Sucesso!!","Informa\u00E7\u00E3o",JOptionPane.INFORMATION_MESSAGE);
						
						BloquearCampos();
						btnFecharChamado.setEnabled(false);
						
					} catch (NumberFormatException e2) {
						JOptionPane.showMessageDialog(null,"Problema na Altera\u00E7\u00E3o do Chamado: "
								+ e2.getMessage());
					} catch (ClassNotFoundException e2) {
						JOptionPane.showMessageDialog(null,"Problema na Altera\u00E7\u00E3o do Chamado: "
								+ e2.getMessage());
					} catch (SQLException e2) {
						JOptionPane.showMessageDialog(null,"Problema na Altera\u00E7\u00E3o do Chamado: "
								+ e2.getMessage());
					}
					
					
				}

			}
		});
	}
}
